package com.elegantsoft.pakistannews;

import java.util.ArrayList;
import java.util.Arrays;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.atiffarrukh.pakistannews.R;
import com.elegantsoft.pakistannews.rssfeed.MessageList;
import com.google.analytics.tracking.android.EasyTracker;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Channel extends SherlockActivity {
	String cat;
	boolean english;
	SharedPreferences prefs;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		Bundle extras = getIntent().getExtras();
		english = extras.getBoolean("english");
		Log.d("English Selected", "" +english);
		cat = extras.getString("category");

		if (english) {
			if (cat.equals("Headlines")) {
				String[] channel = { "Geo", 
						"Express Tribune", "PTV News", "Aaj News", "The News","Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("Pakistan")) {
				String[] channel = { "Geo", 
						"Express Tribune", "PTV News", "Aaj News", "The News","Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("World")) {
				String[] channel = { "Geo", "Express Tribune",
						"The News","Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("Buisness")) {
				String[] channel = { "Geo", "Express Tribune",
						 "The News", "Buisness Recorder","Selected Channels" };
				listViewPopulate(channel);
			} else if (cat.equals("Sports")) {
				String[] channel = { "Geo", "Express Tribune",
						"ESPN Pakistan Cricket","The News","Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("Entertainment")) {
				String[] channel = { "Geo","Express Tribune","Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("Health")) {
				String[] channel = { "Geo","Selected Channels" };
				listViewPopulate(channel);
			} else if (cat.equals("Science")) {
				String[] channel = { "Popular Science", "BBC" ,"Selected Channels"};
				listViewPopulate(channel);
			} else if (cat.equals("Technology")) {
				String[] channel = { "Popular Science", "BBC",
						"Pro Pakistani","Selected Channels"  };
				listViewPopulate(channel);
			}
		}else {
			if (cat.equals("Headlines")) {
				String[] channel = {"Waqt","Nawai Waqt Urdu", "Jang News Urdu" };
				listViewPopulate(channel);
			} else if (cat.equals("Pakistan")) {
				String[] channel = { "Waqt","Nawai Waqt Urdu", "Jang News Urdu"  };
				listViewPopulate(channel);
			} else if (cat.equals("World")) {
				String[] channel = { "Jang News Urdu" };
				listViewPopulate(channel);
			} else if (cat.equals("Buisness")) {
				String[] channel = {"Jang News Urdu" };
				listViewPopulate(channel);
			} else if (cat.equals("Sports")) {
				String[] channel = { "Jang News Urdu" };
				listViewPopulate(channel);
			} 
	
		}
	}

	private void listViewPopulate(String[] channel) {
		ListView lv = (ListView) findViewById(R.id.catList);

		ArrayList<String> channelList = new ArrayList<String>();
		channelList.addAll(Arrays.asList(channel));

		MyCustommAdapter adapter = new MyCustommAdapter(this,channel);

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> paren, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
				if(isConnected()){
					TextView tv = (TextView) view.findViewById(R.id.text1);
					String channelString = tv.getText().toString();
					//if(channelString.equals("Selected Channels") && prefs.getBoolean("activate",false)){
						Intent i = new Intent(getApplicationContext(),
								MessageList.class);
						i.putExtra("channelName", channelString);
						i.putExtra("category", cat);
						i.putExtra("alternate", false);
						i.putExtra("english", english);
						startActivity(i);
					
				}else
					Toast.makeText(getApplicationContext(),
							Farsi.Convert("You are not connected to Internet"),
							Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	// Check the connectivity with internet
		boolean isConnected() {
			ConnectivityManager conMgr = (ConnectivityManager) this
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo i = conMgr.getActiveNetworkInfo();
			if (i == null)
				return false;
			if (!i.isConnected())
				return false;
			if (!i.isAvailable())
				return false;
			return true;
		}
		
		public boolean onOptionsItemSelected(MenuItem item) {
		    switch (item.getItemId()) {

		    case android.R.id.home:
		         // Do whatever you want, e.g. finish()
		    	Intent i = new Intent(this, MainActivity.class);
		    	i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		    	i.putExtra("category",cat );
		    	i.putExtra("english", english);
		    	startActivity(i);
		    	finish();
		        return true; 
		    	

		    default:
		        return super.onOptionsItemSelected(item);
		    }
		}
		 @Override
		  public void onStart() {
		    super.onStart();
		  
		   // EasyTracker.getInstance().activityStart(this); // Google Analytics.
		  }

		  @Override
		  public void onStop() {
		    super.onStop();
		    
		   // EasyTracker.getInstance().activityStop(this); // Google Analytics.
		  }

}
